//
//  DestinationViewController.m
//  TravelToHelp
//
//  Created by lanou3g on 15/10/28.
//  Copyright © 2015年 冯贵宾. All rights reserved.
//

#import "DestinationViewController.h"
#import "DestinationAreaModel.h"
#import "DestinationCityViewController.h"
#import "AmbitusListViewController.h"

@interface DestinationViewController ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>


@property (nonatomic, strong) NSMutableArray *areaDataArray;
@property (nonatomic, strong) UICollectionView *collectionView;

@end

@implementation DestinationViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.edgesForExtendedLayout = UIRectEdgeNone;
    [self.tabBarController.tabBar setTranslucent:NO];
    self.view.backgroundColor = [UIColor whiteColor];
    [self setUpData];
    [self addSubView];
}

#pragma mark---处理数据
- (void)setUpData
{
    NSString *path = [[NSBundle mainBundle] pathForResource:@"DestinationCityList" ofType:@"plist"];
    NSArray *tempArray = [NSArray arrayWithContentsOfFile:path];
    self.areaDataArray = [NSMutableArray array];
    for (NSDictionary *dict in tempArray) {
        DestinationAreaModel *model = [[DestinationAreaModel alloc] init];
        [model setValuesForKeysWithDictionary:dict];
        [self.areaDataArray addObject:model];
    }
}
#pragma mark ---布局控件
- (void)addSubView
{
    [self SetNavigationBar];
    [self addCollectionView];
  
}


- (void)addCollectionView
{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    // 设置行间距
    layout.minimumLineSpacing = 10;
    //layout.minimumInteritemSpacing = 10;
    // 设置item的宽高
    layout.itemSize = CGSizeMake((self.view.width - 30) / 2, (self.view.width - 30) / 2 * 1.3);
    
    // 设置滑动方向 上下滑动
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    // 设置内边距
    layout.sectionInset = UIEdgeInsetsMake(10, 10, 0, 10);
    // 利用layout创建一个视图
    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - 113) collectionViewLayout:layout];
    collectionView.showsVerticalScrollIndicator = NO;
    collectionView.backgroundColor = [UIColor whiteColor];
    // 设置上面的图片
    collectionView.contentInset = UIEdgeInsetsMake(170, 0, 0, 0);
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, -160, kScreenWidth - 20, 160)];
    imageView.backgroundColor = [UIColor whiteColor];
    imageView.layer.cornerRadius = 10;
    imageView.layer.masksToBounds = YES;
    imageView.image = [UIImage imageNamed:@"DestinationTop.jpg"];
    [collectionView addSubview:imageView];
    
    for (int i = (imageView.height - 60) / 2; i < (imageView.height - 60) / 2 + 60; i ++) {  // 50虚化的高度与颜色深浅
        CGFloat num = 1.0 / 30 *(30 - i);
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, (imageView.height - 60) / 2 - i, imageView.width, 1)];   // _coverImageView背景图片
        view.backgroundColor = [UIColor colorWithRed:0 / 255.0 green:0 / 255.0 blue:0 / 255.0  alpha:num];
        [imageView addSubview:view];
    }
    
    
    
    UILabel *alable = [[UILabel alloc] initWithFrame:CGRectMake(0, (imageView.height - 60) / 2, imageView.width, 60)];
    alable.textAlignment = NSTextAlignmentCenter;
    alable.numberOfLines = 0;
    alable.text = @"点击下方热门目的地\n开始你的旅程";
    alable.font = [UIFont systemFontOfSize:25];
    alable.textColor = [UIColor whiteColor];
    [imageView addSubview:alable];
    
    // 设置代理
    collectionView.delegate = self;
    collectionView.dataSource = self;
    self.collectionView = collectionView;
    // 显示视图
    [self.view addSubview:self.collectionView];
    
    //    // 注册cell
    [collectionView registerClass:[DetinationAreaCell class] forCellWithReuseIdentifier:@"destinationCityCell"];
}
#pragma mark --collectionView的代理方法
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.areaDataArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
   DetinationAreaCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"destinationCityCell" forIndexPath:indexPath];
    
  
        if (self.areaDataArray.count > 0) {
            DestinationAreaModel *model = self.areaDataArray[indexPath.row];
            cell.model = model;
        }
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    [collectionView deselectItemAtIndexPath:indexPath animated:YES];
  DestinationAreaModel *model = self.areaDataArray[indexPath.row];
  DestinationCityViewController *dcVC = [[DestinationCityViewController alloc] init];
    dcVC.hidesBottomBarWhenPushed = YES;
   dcVC.urlString = model.url;
 [self.navigationController pushViewController:dcVC animated:YES];
}

- (void)SetNavigationBar
{
    self.navigationItem.title = @"目的地";
       [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"TestNaviBar1.jpg"] forBarMetrics:(UIBarMetricsDefault)];
}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
